package ordercenter

import (
	"fmt"
	"time"
)

type Order struct {
	ID          string
	Type        string
	UserID      string
	Items       []Item
	TotalAmount float64
	Discount    float64
	FinalAmount float64
	Status      string
	CreatedAt   time.Time
}

// 订单项
type Item struct {
	ProductID string
	Name      string
	Price     float64
	Quantity  int
}

// 订单处理器接口
type OrderProcessor interface {
	ValidateOrder(order *Order) error
	CalculatePrice(order *Order)
	ApplyDiscounts(order *Order)
	DeductInventory(order *Order) error
	GeneratePayment(order *Order) error
	NotifyUser(order *Order)
	LogOrder(order *Order)
}

// 模板方法 - 定义订单处理流程
func ProcessOrder(order *Order, processor OrderProcessor) error {
	fmt.Printf("\n开始处理 %s 订单: %s\n", order.Type, order.ID)
	
	// 步骤1: 验证订单
	if err := processor.ValidateOrder(order); err != nil {
		return err
	}
	
	// 步骤2: 计算价格
	processor.CalculatePrice(order)
	
	// 步骤3: 应用折扣
	processor.ApplyDiscounts(order)
	
	// 步骤4: 扣减库存
	if err := processor.DeductInventory(order); err != nil {
		return err
	}
	
	// 步骤5: 生成支付
	if err := processor.GeneratePayment(order); err != nil {
		return err
	}
	
	// 步骤6: 通知用户
	processor.NotifyUser(order)
	
	// 步骤7: 记录日志
	processor.LogOrder(order)
	
	fmt.Printf("订单 %s 处理完成\n", order.ID)
	return nil
}